﻿using System;
using System.Text.RegularExpressions;
using System.Web;

namespace Chengqbd.Core.Extensions
{
	public static class ValidExtensions
	{
		public static bool IsMatch(this string value, string pattern)
		{
			bool flag = value == null;
			return !flag && Regex.IsMatch(value, pattern);
		}
        
		public static bool IsMatch(this string value, string pattern, RegexOptions options)
		{
			bool flag = value == null;
			return !flag && Regex.IsMatch(value, pattern, options);
		}
        
		public static bool IsUrl(this string url)
		{
			return Regex.IsMatch(url, "^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\\\\.))+(([a-zA-Z0-9\\\\._-]+\\\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\\\.[0-9]{1,3}\\\\.[0-9]{1,3}\\\\.[0-9]{1,3}))(/[a-zA-Z0-9\\\\&amp;%_\\\\./-~-]*)?$", RegexOptions.IgnoreCase);
		}
        
		public static bool IsIP(this string ip)
		{
			bool flag = string.IsNullOrEmpty(ip);
			return !flag && Regex.IsMatch(ip, "^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$", RegexOptions.IgnoreCase);
		}
        
		public static bool IsPhone(this string phone)
		{
			bool flag = string.IsNullOrEmpty(phone);
			return !flag && Regex.IsMatch(phone, "^(\\+86)?1[3,4,5,6,7,8,9](\\d{9})$");
		}
        
		public static bool IsEmail(this string email)
		{
			bool flag = string.IsNullOrEmpty(email);
			return !flag && Regex.IsMatch(email, "^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\\.[a-zA-Z0-9_-]{2,3}){1,2})$", RegexOptions.IgnoreCase);
		}
        
		public static bool IsGuid(this string guid)
		{
			bool flag = string.IsNullOrWhiteSpace(guid) || guid.Length != 36;
			return !flag && Regex.IsMatch(guid, "[0-9|a-f|A-F]{8}-[0-9|a-f|A-F]{4}-[0-9|a-f|A-F]{4}-[0-9|a-f|A-F]{4}-[0-9|a-f|A-F]{12}", RegexOptions.IgnoreCase);
		}
        
		public static bool IsNum(this string str)
		{
			bool flag = string.IsNullOrWhiteSpace(str);
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				string pattern = "^-?\\d+$";
				result = Regex.IsMatch(str, pattern);
			}
			return result;
		}
        
		public static bool IsChinese(this string str)
		{
			bool flag = string.IsNullOrWhiteSpace(str);
			return !flag && Regex.IsMatch(str, "^[一-龥]+$");
		}
        
		public static bool IsOdd(this int Num)
		{
			return Convert.ToBoolean(Num % 2);
		}
        
		public static bool IsDate(this string str)
		{
			return Regex.IsMatch(str, "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$");
		}
        
		public static bool IsIdCard(this string str)
		{
			long num = 0L;
			bool flag = str.IsNullOrWhiteSpace() || str.Length < 18 || !long.TryParse(str.Remove(17), out num) || (double)num < Math.Pow(10.0, 16.0);
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				bool flag2 = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91".IndexOf(str.Remove(2)) == -1;
				if (flag2)
				{
					result = false;
				}
				else
				{
					string s = str.Substring(6, 8).Insert(6, "-").Insert(4, "-");
					DateTime dateTime = default(DateTime);
					bool flag3 = !DateTime.TryParse(s, out dateTime);
					if (flag3)
					{
						result = false;
					}
					else
					{
						string[] array = "1,0,x,9,8,7,6,5,4,3,2".Split(new char[]
						{
							','
						});
						string[] array2 = "7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2".Split(new char[]
						{
							','
						});
						char[] array3 = str.Remove(17).ToCharArray();
						int num2 = 0;
						for (int i = 0; i < 17; i++)
						{
							num2 += int.Parse(array2[i]) * int.Parse(array3[i].ToString());
						}
						int num3 = -1;
						Math.DivRem(num2, 11, out num3);
						bool flag4 = string.Compare(array[num3], str.Substring(17, 1), true) != 0;
						result = !flag4;
					}
				}
			}
			return result;
		}
        
		public static string HtmlDecode(this string html)
		{
			return Regex.Replace(html, "<[^>]+>", "");
		}
        /// <summary>
        /// 处理url非法字符
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string UrlEnCode(this string url)
        {
            if (url.IsUrl())
                return HttpUtility.UrlEncode(url);
            else
                return url;
        }

		public static bool IsCardNo(this string cardNo)
		{
			return Regex.IsMatch(cardNo, "^[0-9]+$");
		}
	}
}
